using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using EPiAbstractions.Common.Tags;
using EPiServer.Common.Categories;
using EPiServer.Common.Tags;
using EPiServer.Community.Blog;
using JoelAbrahamsson.BlogExtensions;
using JoelAbrahamsson.Web.Common;
using JoelAbrahamsson.Web.ExtensionMethods;

namespace JoelAbrahamsson.Web.Controllers
{
    public class FooterController : Controller
    {
        private ExtendedBlogHandlerFacade _blogHandlerFacade;
        private BlogCategoryProvider _blogCategoryProvider;
        private ITagHandlerFacade _tagHandlerFacade;

        public FooterController(ExtendedBlogHandlerFacade blogHandlerFacade, BlogCategoryProvider blogCategoryProvider, ITagHandlerFacade tagHandlerFacade)
        {
            _blogHandlerFacade = blogHandlerFacade;
            _blogCategoryProvider = blogCategoryProvider;
            _tagHandlerFacade = tagHandlerFacade;
        }

        public ActionResult Index(Blog blog)
        {
            int totalItems;
            ViewData["latestEntries"] = _blogHandlerFacade.GetLatestEntries(blog, 1, 5, out totalItems);

            TagCloud tagCloud = _tagHandlerFacade.GetTagCloud(int.MaxValue - 1);
            ViewData["tags"] = tagCloud;

            IList<ICategory> categories = new List<ICategory>();
            foreach(ICategory child in _blogCategoryProvider.BlogCategoriesRoot.Children)
            {
                foreach (ICategory grandChild in child.Children)
                {
                    categories.Add(grandChild);
                }
            }

            List<LinkGroup> categoryGroups = GetCategoryGroups();
            ViewData["categories"] = categoryGroups;

            return View();
        }

        private List<LinkGroup> GetCategoryGroups()
        {
            List<LinkGroup> categoryGroups = new List<LinkGroup>();
            foreach (ICategory mainCategory in _blogCategoryProvider.BlogCategoriesRoot.Children)
            {
                LinkGroup group = new LinkGroup
                {
                    Name = mainCategory.Name,
                    Links = new List<Link>()
                };
                foreach (ICategory childCategory in mainCategory.Children)
                {
                    if(childCategory.GetEntryCount() == 0)
                        continue;

                    MenuLink categoryLink = new MenuLink
                    {
                        Text = childCategory.Name,
                        RouteValues = new RouteValueDictionary(new
                        {
                            controller = "Archive",
                            action = "Category",
                            mainCategoryName = mainCategory.GetSlug(),
                            subCategoryName = childCategory.GetSlug()
                        })
                    };

                    group.Links.Add(categoryLink);
                }
                categoryGroups.Add(group);
            }
            return categoryGroups;
        }
    }
}
